﻿<?
class os
{
    var $return_array = array(); // 返回带有MAC地址的字串数组
    var $mac_addr = array();
    var $cpuinfo = array();
    var $diskinfo = array();
    public function __construct ($os_type)
    {
        switch (strtolower($os_type)) {
            case "linux":
                $this->getlinuxmac();
                $this->getlinuxcpuinfo();
                $this->getlinuxtdiskinfo();
                break;
            case "solaris":
                break;
            case "unix":
                break;
            case "aix":
                break;
            default:
                $this->getwindowsmac();
                $this->getwindowcpuinfo();
                $this->getwindowsdiskfinfo();
                break;
        }
    }
    public function gettimes ()
    {
        $vkeypath = ROOT . '/date/pri.key';
        $vdtpath = ROOT . '/date/v.dt';
        if (! file_exists($vdtpath)) {
            return FALSE;
        }
        $encrypted = file_get_contents($vdtpath);
        $prk = file_get_contents($vkeypath);
        $privKey = openssl_pkey_get_private($prk);
        $encrypted = base64_decode($encrypted);
        
        $maxlength = 128;
        $decrypted = '';
        while ($encrypted) {
            $input = substr($encrypted, 0, $maxlength);
            $encrypted = substr($encrypted, $maxlength);
            $r = openssl_private_decrypt($input, $out, $privKey);
            $decrypted .= $out;
        }
        
       
        if (! $r) {
            return false;
        }
        $decryptedar = explode('~^@^~', $decrypted);
        if (! is_array($decryptedar))
            return false;
             #0 macode 1 num 2,sorts
         $infoar = explode('-', $decryptedar[0]);
         
        if (count($infoar)==4 && $infoar[3]<>'') {
        	$i=0;
            while ($i < 10000) {
                if (md5($i) == $infoar[3]) {
                    return $i;
                }
                $i++;
            }
            return false;
        }
        return false;
    }
    private function getlinuxmac ()
    {
        @ exec("/sbin/ifconfig -a 2>&1", $acar);
        $temp_array = array();
        foreach ($acar as $acarv) {
            if (preg_match(
            "/[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f]/i", $acarv, $temp_array)) {
                $this->mac_addr = $temp_array;
                break;
            }
        }
    }
    private function getwindowsmac ()
    {
        @exec("ipconfig /all", $acar);
        foreach ($acar as $acarv) {
            if (preg_match(
            "/[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f]/i", $acarv, $temp_array)) {
                $this->mac_addr = $temp_array;
                break;
            }
        }
    }
    function getlinuxcpuinfo ()
    {
        @exec("/usr/bin/cpuid ", $cpuinfo);
        foreach ($cpuinfo as $cpuinfov) {
            if (preg_match(
            "/[0-9a-f][0-9a-f][0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][0-9a-f][0-9a-f]/i", $cpuinfov, $temp_array)) {
                $this->cpuinfo = $temp_array;
                break;
            }
        }
    }
    function getwindowcpuinfo ()
    {
        @exec("systeminfo ", $cpuinfo);
        foreach ($cpuinfo as $cpuinfov) {
            if (preg_match("/.*Mhz/i", $cpuinfov, $temp_array)) {
                $this->cpuinfo = $temp_array;
                break;
            }
        }
    }
    function getlinuxtdiskinfo ()
    {
        @exec("/sbin/hdparm -i /dev/sda 2>&1", $diskinfoar);
        if (is_array($diskinfoar)) {
            #SerialNo=QM00001
            $temp = implode('', $diskinfoar);
            preg_match("/SerialNo.*? /i", $temp, $temp_array);
            # $temp = 
            $this->diskinfo = $temp_array;
        } else {
            $this->diskinfo = array('10000');
        }
    }
    function getwindowsdiskfinfo ()
    {
        @exec("ipconfig/all ", $diskinfoar);
        foreach ($diskinfoar as $diskinfov) {
            if (preg_match(
            "/[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f][:-]" .
             "[0-9a-f][0-9a-f][:-]" . "[0-9a-f][0-9a-f]/i", $diskinfov, 
            $temp_array)) {
                $this->diskinfo = $temp_array;
                break;
            }
        }
    }
}
//$os = new os(PHP_OS);
//var_dump($os->mac_addr);
//var_dump($os->cpuinfo);
//var_dump($os->diskinfo);
?>